package cn.itcast.bos.web.action;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.ws.rs.core.MediaType;

import org.apache.commons.lang3.StringUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.hibernate.stat.SecondLevelCacheStatistics;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import cn.itcast.bos.constant.Constants;
import cn.itcast.bos.domain.base.FixedArea;
import cn.itcast.bos.service.FixedAreaService;
import cn.itcast.bos.web.action.common.BaseAction;
import cn.itcast.crm.domain.Customer;

@ParentPackage(value="json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class FixedAreaAction extends BaseAction<FixedArea> {

	@Autowired
	private FixedAreaService fixedAreaService;
	
	
	@Action(value="fixedArea_save",results= {@Result(name="success",type="redirect",location="./pages/base/fixed_area.html")})
	public String save()  {
		fixedAreaService.save(model);
		return SUCCESS;
	}
	
	/**
	 * 没有条件的分页查询
	 */
	@Action(value="fixedArea_pageQuery",results= {@Result(name="success",type="json")})
	public String pageQuery() {
		/**
		 * 有条件查询
		 * 
		 */
		//调用service层,查询返回数据
		Pageable pageable=new PageRequest(page-1, rows);
		//组织查询条件
		Specification<FixedArea> specification=new Specification<FixedArea>() {

			@Override
			public Predicate toPredicate(Root<FixedArea> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				 Predicate predicate =null;
				List<Predicate> list=new ArrayList<Predicate>();
				//id编号
				if (StringUtils.isNotBlank(model.getId())) {
					Predicate p = cb.like(root.get("id").as(String.class), model.getId());
					list.add(p);
				}
				//所属单位
				if (StringUtils.isNotBlank(model.getCompany())) {
					Predicate p = cb.like(root.get("company").as(String.class), "%"+model.getCompany()+"%");
					list.add(p);
				}
				if (list!=null&&list.size()>0) {
					Predicate [] p=new Predicate[list.size()];
					predicate=cb.and(list.toArray(p));
				}
				return predicate;
			}
		};
		Page<FixedArea> pageData= fixedAreaService.findPageDataFixedArea(specification, pageable);                  
		//返回客户端数据
		pushPageDataToValueStack(pageData);
		return SUCCESS;
	}
	
	//查找没有关联定区的客户
	@Action(value="fixedArea_findNoAssociationCustomers",results= {@Result(name="success",type="json")})
	public String findNoAssociationCustomers() {
		Collection<? extends Customer> collection = WebClient
				.create("http://localhost:9002/crm_management/services/customerService/noassociationcustomers")
				.accept(MediaType.APPLICATION_JSON)
				.getCollection(Customer.class);
		ActionContext.getContext().getValueStack().push(collection);
		return SUCCESS;
		
	}
	// 查询关联当前定区的列表
		@Action(value = "fixedArea_findHasAssociationFixedAreaCustomers", results = { @Result(name = "success", type = "json") })
		public String findHasAssociationFixedAreaCustomers() {
			//System.out.println(model.getId()+"*******");
			// 使用webClient调用 webService接口
			Collection<? extends Customer> collection = WebClient
					.create(Constants.CRM_MANAGEMENT_URL+"/services/customerService/associationfixedareacustomers/"
							+ model.getId()).accept(MediaType.APPLICATION_JSON)
					.type(MediaType.APPLICATION_JSON).getCollection(Customer.class);
			ActionContext.getContext().getValueStack().push(collection);
			return SUCCESS;
		}
	/**
	 * 进行区域的关联操作
	 * 
	 */
		//属性驱动
		private String[] customerIds;

		public void setCustomerIds(String[] customerIds) {
			this.customerIds = customerIds;
		}
		
		//关联客户到定去
		@Action(value = "fixedArea_associationCustomersToFixedArea", results = { @Result(name = "success", 
				type = "redirect" ,location="./pages/base/fixed_area.html") })
		public String associationCustomersToFixedArea() {
			String customerIdStr = StringUtils.join(customerIds,",");
			customerIdStr=customerIdStr==null?"":customerIdStr;
			WebClient.create(Constants.CRM_MANAGEMENT_URL+"/services/customerService"
					+ "/associationcustomerstofixedarea?customerIdStr="
					+ customerIdStr + "&fixedAreaId="+model.getId()
					).put(null);
			return SUCCESS;
		}
	
		/**
		 * 定区关联快递员
		 * 
		 */
	
		//属性驱动获取courierId,takeTimeId
		// 快递员ID
		private String courierId;
		//收派时间ID
		private String takeTimeId;
		public void setCourierId(String courierId) {
			this.courierId = courierId;
		}
		public void setTakeTimeId(String takeTimeId) {
			this.takeTimeId = takeTimeId;
		}
		@Action(value = "fixedArea_associationCourierToFixedArea", 
				results = { @Result(name = "success", type = "redirect", location = "./pages/base/fixed_area.html") })
		public String associationCourierToFixedArea() {
			fixedAreaService.associationCourierToFixedArea(model,courierId,takeTimeId);
			return SUCCESS;
			
		}
	
	
	
	
}
